﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Emgu.CV.UI;
using Emgu.CV;
using Emgu.CV.Structure;

namespace EmguWPFCamera
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class DisplayCameraControl : UserControl
    {
        private ImageBox display;

        public ImageBox Display
        {
            get
            {
                return display;
            }
            set
            {
                this.display = value;
            }
        }

        public Emgu.CV.Image<Bgr,Byte> Image
        {
            get
            {
                if (display == null)
                    return null;
                else
                    return display.Image as Image<Bgr, Byte>;
            }
            set
            {
                if (display != null)
                {
                    bool resize = false;
                    int width = (int)this.Width;
                    int height = (int)this.Height;
                    if (value.Width != width)
                        resize = true;
                    if (value.Height != height)
                        resize = true;
                    if (resize)
                    {
                        display.Image = value.Resize(width, height, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                    }
                    else
                        display.Image = value.Clone();
                }
            }
        }

        public DisplayCameraControl()
        {
            InitializeComponent();
            display = new ImageBox();
            ((System.ComponentModel.ISupportInitialize)(display)).BeginInit();
            display.FunctionalMode = ImageBox.FunctionalModeOption.Minimum;
            display.Location = new System.Drawing.Point(0, 0);
            display.TabIndex = 0;
            display.TabStop = false;
            ImageBoxHost.Child = display;
        }

        
    }
}
